package com.project.dao.imp;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.dao.BaseDao;
import com.project.dao.ILinkDao;
import org.springframework.stereotype.Repository;


import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class LinkDaoImp extends BaseDao implements ILinkDao {
    @Override
    public CutPageBean<LinkBean> cutAll(int pageNO) {
        CutPageBean<LinkBean> cutBean = new CutPageBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_link limit ?,?");
            ps.setObject(1, (pageNO - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2, CutPageBean.PAGESIZE);

            rs = ps.executeQuery();
            while (rs.next()) {
                LinkBean linkBean = new LinkBean();
                linkBean.setId(rs.getInt("pk_linkId"));
                linkBean.setName(rs.getString("l_name"));
                linkBean.setBirthday(LocalDate.parse(rs.getString("l_birthday")));
                linkBean.setPhone(rs.getString("l_phone"));
                cutBean.getList().add(linkBean);
            }

            ps = con.prepareStatement("select  count(*) c from t_link ");
            rs = ps.executeQuery();
            if (rs.next()) {
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }

        return cutBean;
    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("DELETE from t_link where pk_linkId=?");

            ps.setObject(1, id);

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void add(LinkBean user) {
        this.setConnection();
        try {
            this.ps = this.con.prepareStatement("insert into t_link(l_name,l_birthday,l_phone) values (?,?,?)");
            this.ps.setObject(1, user.getName());
            this.ps.setObject(2, user.getBirthday());
            this.ps.setObject(3, user.getPhone());
            this.ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public LinkBean findById(int id) {
        this.setConnection();
        try {
            this.ps = this.con.prepareStatement("select * from t_link where pk_linkId = ?");
            this.ps.setObject(1, id);
            this.rs = this.ps.executeQuery();
            if (rs.next()) {
                LinkBean linkBean = new LinkBean();
                linkBean.setId(this.rs.getInt("pk_linkId"));
                linkBean.setName(this.rs.getString("l_name"));
                String bir = rs.getString("l_birthday");
                linkBean.setBirthday(bir != null ? LocalDate.parse(bir) : null);
                linkBean.setPhone(this.rs.getString("l_phone"));
                return linkBean;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return null;
    }

    @Override
    public void update(int id, String phone) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_link set l_phone=? where pk_linkId=?");

            ps.setObject(1, phone);
            ps.setObject(2, id);

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }


}
